//
//  LSSubjDetailViewController.m
//  NineGroupProject
//
//  Created by dllo on 15/12/23.
//  Copyright © 2015年 lushan. All rights reserved.
//

#import "LSSubjDetailViewController.h"
#import "SubjectModel.h"
#import <Masonry.h>
#import "LSCollectionDataBase.h"
@interface LSSubjDetailViewController ()

@property (nonatomic, strong)UIButton *saveBtn;

@end

@implementation LSSubjDetailViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.
    
    self.navigationItem.leftBarButtonItem = [[UIBarButtonItem alloc] initWithTitle:@"  返回" style:UIBarButtonItemStylePlain target:self action:@selector(leftAction)];
    
    [[LSCollectionDataBase shareDataBase] openDB];
    [[LSCollectionDataBase shareDataBase]createTable];
    
    self.saveBtn = [UIButton buttonWithType:UIButtonTypeCustom];
    self.saveBtn.frame = CGRectMake(0, 0, 30 * WIDTH / 375, 30 * HEIGHT / 667);
    [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxing.png"] forState:UIControlStateNormal];
    [self.saveBtn addTarget:self action:@selector(rightAction) forControlEvents:UIControlEventTouchUpInside];
    self.navigationItem.rightBarButtonItem = [[UIBarButtonItem alloc] initWithCustomView:self.saveBtn];
    
    NSInteger urlId = self.subTVM.experience_id;
    NSString *string = [NSString stringWithFormat:@"http://www.daizhe.cn/experience/%ld",urlId];
    self.webView = [[UIWebView alloc]init];
    self.webView.backgroundColor = [UIColor clearColor];
//    self.webView.delegate = self;
    
//    self.webView.opaque = NO; //不设置这个值 页面背景始终是白色
//    self.webView.scalesPageToFit = NO;  //禁止用户缩放页面
    
    NSURLRequest *request = [NSURLRequest requestWithURL:[NSURL URLWithString:string]];
    [self.view addSubview:self.webView];
    [self.webView loadRequest:request];
    
    [_webView mas_makeConstraints:^(MASConstraintMaker *make) {
        make.left.mas_equalTo(0);
        make.bottom.mas_equalTo(0);
        make.top.mas_equalTo(0);
        make.right.mas_equalTo(0);
    }];
    
}
- (void)leftAction{
    CATransition *transition = [CATransition animation];
    transition.duration = 0.7f;
    transition.timingFunction = [CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut];
    transition.type = @"cube";
    transition.subtype = kCATransitionFromLeft;
    transition.delegate = self;
    [self.navigationController.view.layer addAnimation:transition forKey:nil];
    [self.navigationController popViewControllerAnimated:YES];
}
- (void)rightAction{
    
    
    
    NSMutableArray *array = [[LSCollectionDataBase shareDataBase] select:self.subTVM.experience_id];
    if (array.count == 0) {
        [[LSCollectionDataBase shareDataBase] insertSub:self.subTVM.experience_id Title:self.subTVM.experience_title Img:self.subTVM.relate_img];
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"收藏成功" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            
            //关键帧动画
            CAKeyframeAnimation *keyAnimation = [CAKeyframeAnimation animationWithKeyPath:@"transform.scale"];
            //设置关键帧的值,values是数组类型
            keyAnimation.values = @[@(0.1),@(1.0),@(1.5)];
            keyAnimation.duration = 0.3;
            [self.saveBtn.layer addAnimation:keyAnimation forKey:@"key"];
            [self.saveBtn setBackgroundImage:[UIImage imageNamed:@"iconfont-xinxing-2.png"] forState:UIControlStateNormal];
            
            NSLog(@"OK");
        }];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];
    }else{
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"" message:@"已收藏" preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *canact = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleCancel handler:^(UIAlertAction *action) {
            NSLog(@"取消");
        }];
        UIAlertAction *othact = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction *action) {
            NSLog(@"OK");
        }];
        [alert addAction:canact];
        [alert addAction:othact];
        [self presentViewController:alert animated:YES completion:nil];

    }
    
    
    
    
    
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
